package edu.uhcl.csci4838.group3.contentstorage;
import java.io.File;
import java.io.FileWriter;

import android.content.Context;
import android.util.Log;

/**
 * This FileManager will manage the internal SD data storage for semesters and
 * classes. It will provide write and delete methods for
 * './semester_unique_name/class_unique_name' location.
 * 
 * @author Paul Bradt
 */

public class DefaultFileManager {
	
	/**
	 * XXX
	 */
	private Context context;
	
	/**
	 * dirName This hold the actual directory path name on the SD card.
	 */
	
	private final String dirName;
	
	/**
	 * fileName fileName is the actual file name passed to this class by the
	 * constructor.
	 */
	
	private String fileName;
	
	/**
	 * Paul Bradt semesterName and courseName for the public constructor will
	 * set the location for access to file for create, read, update, and delete.
	 * 
	 * @param context
	 * @param semesterName
	 *            Take a string value and use it to create or access existing
	 *            folder path.
	 * @param courseName
	 *            Take a string value and use it to create or access existing
	 *            folder path.
	 * @param fileName
	 *            The actual file name in format of "filename.txt".
	 */
	
	public DefaultFileManager(final Context context,
			final String semesterName,
			final String courseName,
			final String fileName) {
		this.dirName = (semesterName
				+ File.separator
				+ courseName
				+ File.separator + fileName);
	}
	
	/**
	 * Paul Bradt Method deleteFile will use the default constructor's fileName
	 * to locate the file. XXX
	 * 
	 * @return This method will return true if delete operation was successful
	 *         otherwise return false.
	 */
	public boolean deleteFile() {
		
		try {
			
			final File filesDir = this.context.getFilesDir();
			final File dir = new File(filesDir.getAbsolutePath()
					+ File.separator
					+ this.dirName
					+ File.separator
					+ this.fileName);
			
			if (dir.exists() == true) {
				dir.delete();
				return true;
			}
			
		} catch (final Exception e) {
			Log.d("deafultFileManager", //$NON-NLS-1$
					e.getMessage());
		}
		return false;
		
	}
	
	/**
	 * Returns this.fileName.
	 * 
	 * @return this.fileName
	 * @see #setFileName
	 */
	public String getFileName() {
		return this.fileName;
	}
	
	/**
	 * Sets this.fileName.
	 * 
	 * @param fileName
	 *            The string fileName is used to name the file for write and
	 *            delete purpose.
	 * @see #getFileName
	 */
	public void setFileName(final String fileName) {
		this.fileName = fileName;
	}
	
	/**
	 * Paul Bradt Method writeToFile will take a parameter string and write it
	 * to file.
	 * 
	 * @param fileContent
	 *            fileContent will be used to pass something to be write to
	 *            file.
	 * @return Will return if operation is successfunl otherwise return false.
	 */
	
	public boolean writeToFIle(final String fileContent) {
		
		try {// SD card location/semestername/coursename/note dir/note.txt
		
			final File filesDir = this.context.getFilesDir();
			final File dir = new File(filesDir.getAbsolutePath()
					+ File.separator
					+ this.dirName);
			
			final FileWriter writer = new FileWriter(dir);
			
			writer.write(fileContent);
			writer.flush();
			writer.close();
			return true;
			
		} catch (final Exception e) {
			Log.d("deafultFileManager", //$NON-NLS-1$
					e.getMessage());
		}
		return false;
		
	}
}
